{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第一部分.数据预处理\n",
    "## 1.3.用python创建数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 3.  -1.5  2.  -5.1]\n",
      " [ 0.   4.  -0.3 -2.1]\n",
      " [ 1.   3.3 -1.9 -4.3]]\n",
      "(3, 4)\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "data = np.array([[3,-1.5,2,-5.1],[0,4,-0.3,-2.1],[1,3.3,-1.9,-4.3]])\n",
    "print(data)\n",
    "print(data.shape)# 数据每个维度的带线啊哦"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-5.1, -2.1, -4.3])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[:,-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用arange函数创建一个0-10的一维等差数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "NpArray1=np.arange(0,10)\n",
    "print(NpArray1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90,\n",
       "       95])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "NpArray2 = np.arange(10,100,5)\n",
    "NpArray2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.        ,  0.20408163,  0.40816327,  0.6122449 ,  0.81632653,\n",
       "        1.02040816,  1.2244898 ,  1.42857143,  1.63265306,  1.83673469,\n",
       "        2.04081633,  2.24489796,  2.44897959,  2.65306122,  2.85714286,\n",
       "        3.06122449,  3.26530612,  3.46938776,  3.67346939,  3.87755102,\n",
       "        4.08163265,  4.28571429,  4.48979592,  4.69387755,  4.89795918,\n",
       "        5.10204082,  5.30612245,  5.51020408,  5.71428571,  5.91836735,\n",
       "        6.12244898,  6.32653061,  6.53061224,  6.73469388,  6.93877551,\n",
       "        7.14285714,  7.34693878,  7.55102041,  7.75510204,  7.95918367,\n",
       "        8.16326531,  8.36734694,  8.57142857,  8.7755102 ,  8.97959184,\n",
       "        9.18367347,  9.3877551 ,  9.59183673,  9.79591837, 10.        ])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 使用Linspace函数创建一个有限范围内的等差数组\n",
    "NpArray3 = np.linspace(0,10,50)\n",
    "NpArray3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.4 .用均值移除法(标准化)做数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean: [ 1.33333333  1.93333333 -0.06666667 -3.83333333]\n",
      "Standard Deviation: [1.24721913 2.44449495 1.60069429 1.26841984]\n"
     ]
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "print(\"Mean:\",data.mean(axis=0))\n",
    "print(\"Standard Deviation:\",data.std(axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.33630621, -1.40451644,  1.29110641, -0.99861783],\n",
       "       [-1.06904497,  0.84543708, -0.14577008,  1.36652966],\n",
       "       [-0.26726124,  0.55907936, -1.14533633, -0.36791183]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_scaled = preprocessing.scale(data) # 该函数对数据集上的所有数据轴做标准化操作\n",
    "data_scaled"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mean [ 5.55111512e-17 -1.11022302e-16 -7.40148683e-17  2.40548322e-16]\n",
      "标准差: [1. 1. 1. 1.]\n"
     ]
    }
   ],
   "source": [
    "print(\"Mean\",data_scaled.mean(axis=0))\n",
    "print(\"标准差:\",data_scaled.std(axis=0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.5.数据缩放(MinMax函数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data Min: [ 0.  -1.5 -1.9 -5.1]\n",
      "data.Max: [3.  4.  3.3]\n",
      "data scaled Min [0. 0. 0. 0.]\n",
      "data scaled Max [1. 1. 1. 1.]\n"
     ]
    }
   ],
   "source": [
    "data_scaler = preprocessing.MinMaxScaler(feature_range=(0,1))\n",
    "data_scaled=data_scaler.fit_transform(data)\n",
    "print(\"data Min:\",data.min(axis=0))\n",
    "print(\"data.Max:\",data.max(axis=1))\n",
    "\n",
    "print(\"data scaled Min\",data_scaled.min(axis=0))\n",
    "print(\"data scaled Max\",data_scaled.max(axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.        , 0.        , 1.        , 0.        ],\n",
       "       [0.        , 1.        , 0.41025641, 1.        ],\n",
       "       [0.33333333, 0.87272727, 0.        , 0.26666667]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_scaled"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.6.归一化工作\n",
    "<p>机器学习中最常用的归一化形式是通过调整特征向量的值,让其数值之和为1</p>\n",
    "<P>可使用preprocessing.normalize()进行归一化操作,该函数将输入变量缩放为单位范数(向量长度)。它支持三种类型的范数</P>\n",
    "<UL>\n",
    "    <LI>l1范数:向量内各数值的绝对值之和</LI>\n",
    "    <li>l2范数:向量内部各数值平方和开二次方</li>\n",
    "    <LI>max范数:向量内部的最大值</LI>\n",
    "</UL>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.75       -0.17045455  0.47619048 -0.44347826]\n",
      " [ 0.          0.45454545 -0.07142857 -0.1826087 ]\n",
      " [ 0.25        0.375      -0.45238095 -0.37391304]]\n"
     ]
    }
   ],
   "source": [
    "data_normalized = preprocessing.normalize(data,norm='l1',axis=0)\n",
    "print(data_normalized)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 这个方法经常用于确保数据集不会因为特征的基本性质而被认为地增强"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 1. 1. 1.]\n"
     ]
    }
   ],
   "source": [
    "data_norm_abs = np.abs(data_normalized)\n",
    "print(data_norm_abs.sum(axis=0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.7.二值化\n",
    "<p>二值化用于将数值型特征向量转化为布尔型向量。最典型的应用是图像二值化.即转化为只有两种颜色的图像（黑色和白色）</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 0. 1. 0.]\n",
      " [0. 1. 0. 0.]\n",
      " [0. 1. 0. 0.]]\n",
      "[[ 3.  -1.5  2.  -5.1]\n",
      " [ 0.   4.  -0.3 -2.1]\n",
      " [ 1.   3.3 -1.9 -4.3]]\n"
     ]
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "data_binarized = preprocessing.Binarizer(threshold=1.4).transform(data) #阈值为1.4.大于该参数的值映射为1，小于该参数的值的阈值为0\n",
    "print(data_binarized)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.8 one-hot编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 2],\n",
       "       [0, 2, 3],\n",
       "       [1, 0, 1],\n",
       "       [0, 1, 0]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data=np.array([[1,1,2],[0,2,3],[1,0,1],[0,1,0]])\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 1. 1. 0. 0. 0. 0. 0. 1.]]\n",
      "  (0, 1)\t1.0\n",
      "  (0, 2)\t1.0\n",
      "  (0, 8)\t1.0\n"
     ]
    }
   ],
   "source": [
    "encoder = preprocessing.OneHotEncoder()\n",
    "encoder.fit(data)\n",
    "encoded_vector = encoder.transform([[1,0,3]]).toarray()\n",
    "print(encoded_vector)\n",
    "print(encoder.transform([[1,0,3]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0., 1., 0., 1., 0., 0., 0., 1., 0.],\n",
       "       [1., 0., 0., 0., 1., 0., 0., 0., 1.],\n",
       "       [0., 1., 1., 0., 0., 0., 1., 0., 0.],\n",
       "       [1., 0., 0., 1., 0., 1., 0., 0., 0.]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=encoder.transform(data)\n",
    "a.toarray()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.9标签编码\n",
    "<p>标签编码指的是把文字标签转化为数值形式</p>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "类别映射:\n",
      "\n",
      "audi --> 0\n",
      "bmw --> 1\n",
      "ford --> 2\n",
      "toyota --> 3\n"
     ]
    }
   ],
   "source": [
    "from sklearn import preprocessing\n",
    "label_encoder = preprocessing.LabelEncoder()\n",
    "input_classer = ['audi','ford','audi','toyota','ford','bmw']\n",
    "label_encoder.fit(input_classer) # 拟合标签转换器\n",
    "print('类别映射:\\n')\n",
    "for i,item in enumerate(label_encoder.classes_):\n",
    "    print(item,\"-->\",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Labels= ['toyota', 'ford', 'audi']\n",
      "encoded-labels= [3 2 0]\n"
     ]
    }
   ],
   "source": [
    "labels = ['toyota','ford','audi']\n",
    "encoded_labels=label_encoder.transform(labels)\n",
    "print(\"Labels=\",labels)\n",
    "print(\"encoded-labels=\",encoded_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "encoded_labels [2, 1, 0, 3, 1]\n",
      "decoded_labels ['ford' 'bmw' 'audi' 'toyota' 'bmw']\n"
     ]
    }
   ],
   "source": [
    "# 将数字标签转化为文字标签\n",
    "encoded_labels = [2,1,0,3,1]\n",
    "decoded_labels = label_encoder.inverse_transform(encoded_labels)\n",
    "print(\"encoded_labels\",encoded_labels)\n",
    "print(\"decoded_labels\",decoded_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
